package schema

import (
	"entgo.io/ent"
	"entgo.io/ent/schema/edge"
	"entgo.io/ent/schema/field"
	"entgo.io/ent/schema/index"
	"gitee.com/aldrice/monitor/pkg/validating"
)

// Object holds the schema definition for the Object entity.
type Object struct {
	ent.Schema
}

// Fields of the Object.
func (Object) Fields() []ent.Field {
	return []ent.Field{
		// 基本字段
		field.String("public_address").Validate(validating.ValidateIPFunc).Optional().Comment("服务器公网ip地址"),
		field.String("local_address").Validate(validating.ValidateIPFunc).Optional().Comment("服务器内网ip地址"),
		field.String("nickname").NotEmpty(),

		// agent设置字段
		field.Int("report_interval").Min(19).Default(60).Comment("抓取数据的时间间隔 (抓取粒度), 单位: 秒"),
		field.Int("resend_times").Range(0, 5).Default(1).Comment("agent发送报文出错后的重发次数"),
		field.Int("fetch_timeout").Range(3, 60).Default(3).Comment("抓取数据的超时时间, 单位: 秒"),

		// 身份识别相关
		field.Text("token").Optional(),

		// agent状态字段
		field.Int("agent_version").Optional(),
		field.Time("last_updated_at").Optional(),
	}
}

// Edges of the Object.
func (Object) Edges() []ent.Edge {
	return []ent.Edge{
		edge.From("ruled_by", Rule.Type).Ref("to_object"),
		edge.From("owned_by", User.Type).Ref("have_object"),
		edge.From("aggregated_by", Setting.Type).Ref("with_object"),
		edge.From("has_logs", AlarmLog.Type).Ref("sent_for_object"),
	}
}

func (Object) Indexes() []ent.Index {
	return []ent.Index{
		index.Fields("public_address", "local_address").Unique(),
	}
}
